package com.music.checkpoints.common.service.impl;

import com.music.checkpoints.common.amqp.AmqpExchange;
import com.music.checkpoints.common.amqp.message.EnterMsg;
import com.music.checkpoints.common.config.dao.RedisConfiguration;
import com.music.checkpoints.common.dao.NationExamDetailDao;
import com.music.checkpoints.common.dao.NationExamineeDao;
import com.music.checkpoints.common.dao.NationExamTimeDao;
import com.music.checkpoints.common.dao.NationSignUpDao;
import com.music.checkpoints.common.entity.NationExamDetail;
import com.music.checkpoints.common.entity.NationExaminee;
import com.music.checkpoints.common.entity.NationExamTime;
import com.music.checkpoints.common.entity.NationSignUp;
import com.music.checkpoints.common.enums.ErrorCodeEnum;
import com.music.checkpoints.common.exception.ServiceException;
import com.music.checkpoints.common.service.CacheManager;
import com.music.checkpoints.common.service.ExamineeManager;
import com.music.checkpoints.common.utils.DateUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
public class ExamineeManagerImpl implements ExamineeManager {
    @Autowired
    private NationSignUpDao nationSignUpDao;
    @Autowired
    private NationExamineeDao nationExamineeDao;
    @Autowired
    private NationExamTimeDao nationExamTimeDao;
    @Autowired
    private NationExamDetailDao nationExamDetailDao;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private RedisConfiguration redisConfiguration;

    private static final String HEAD_PREFIX = "https://image.aiyuechen.com/";

    @Override
    public Map<String, Object> login(String examCode) {
        NationSignUp signUp = nationSignUpDao.querySignUpByExamCode(examCode);
        //准考证错误
        if (signUp == null) {
            throw new ServiceException(ErrorCodeEnum.E001.code(), ErrorCodeEnum.E001.getDescription());
        }
        //考试未开始
        if (signUp.getBeginTimeUnix() > DateUtil.getDateline()) {
            throw new ServiceException(ErrorCodeEnum.E002.code(), ErrorCodeEnum.E002.getDescription());
        }
        //考试已结束
        if (signUp.getEndTimeUnix() < DateUtil.getDateline()) {
            throw new ServiceException(ErrorCodeEnum.E003.code(), ErrorCodeEnum.E003.getDescription());
        }
        if (!examCode.equals("123456")) {
            //已经有成绩了
            NationExamDetail detail = nationExamDetailDao.queryByExamCode(examCode);
            if (detail != null) {
                throw new ServiceException(ErrorCodeEnum.E004.code(), ErrorCodeEnum.E004.getDescription());
            }
        }

        NationExaminee nationExaminee = nationExamineeDao.queryById(signUp.getUid());
        String token = UUID.randomUUID().toString();
        //记录参加考试的时间
        amqpTemplate.convertAndSend(AmqpExchange.ENTER_EXAM_TIME,
                AmqpExchange.ENTER_EXAM_TIME + "_ROUTING",
                new EnterMsg(
                        signUp.getUid(),
                        signUp.getExamId(),
                        signUp.getExamCode(),
                        1
                ));
        //记录token
        amqpTemplate.convertAndSend(AmqpExchange.SAVE_ACCESS_TOKEN,
                AmqpExchange.SAVE_ACCESS_TOKEN + "_ROUTING",
                token);
        Map<String, Object> map = new HashMap();
        map.put("head_url", nationExaminee.getHeadUrl());
        map.put("realname", nationExaminee.getRealname());
        map.put("card_num", nationExaminee.getCardNum());
        map.put("uid", nationExaminee.getId());
        map.put("token", token);
        map.put("ticketNum", examCode);

        return map;
    }

    @Override
    public void enterExam(EnterMsg enterMsg) {
        NationExamTime examTime = new NationExamTime();
        examTime.setUid(enterMsg.getUserId());
        examTime.setExamId(enterMsg.getExamId());
        examTime.setExamCode(enterMsg.getExamCode());
        examTime.setType(enterMsg.getType());
        examTime.setCreatedAt(new Timestamp(new Date().getTime()));
        examTime.setUpdatedAt(new Timestamp(new Date().getTime()));
        nationExamTimeDao.initExamTime(examTime);
    }

    @Override
    public void saveToken(String token) {
        StringBuilder sb = new StringBuilder();
        sb.append(redisConfiguration.getRedisPrefix());
        sb.append(redisConfiguration.getAccessToken());
        sb.append(token);
        cacheManager.put(sb.toString(), token, redisConfiguration.getTokenExpire());
    }
}
